<template>
  <div class="btn-icon-select">
    <div class="cap4-mult-select_inner__menu" ref="menu" v-show="isShowMenu">
      <ul>
        <li v-for="item in btnDatas" :key="item.type">
          <BtnItem :item="item" :isColor="false" :buttonType="'div'" />
        </li>
        <li><MoreSetting :isIcon="true" :showPrePigeonhole="true"/></li>
      </ul>
    </div>

      <a-dropdown v-if="orderedBtnList.length" placement="bottomRight">
          <a-menu slot="overlay" id="btn-top-dropdown">
              <a-menu-item v-for="item in orderedBtnList" :key="item.label">
                  <BtnItem v-if="item.handle!=='moreSetting'" :item="item" buttonType="div" :isColor="true"/>
                  <MoreSetting v-else :isIcon="true" :showPrePigeonhole="true"/>
              </a-menu-item>
          </a-menu>
          <div
                  class="btn-icon-select_inner__icon"
                  :title="$i18n('common.display.morebutton.label')"
                 
          >
              <icon-font type="cap-icon-gengduo" />
          </div>
      </a-dropdown>  
  </div>
</template>

<script>
  // 更多设置
  import MoreSetting from '@/components/classic-layout/create/base-info/more-setting';
  import BtnItem from '@/components/classic-layout/create/btn-group-top/components/btn-item';
  import { useNewButtonStore } from '@/store/modules/new-button';
  import { mapState } from 'pinia';
  import activeLock from '@/mixins/summry-lock'
  import emitter from "@/plugins/global-callback";

  export default {
    name: 'btnIconSelect',
    mixins:[activeLock],
    components: {
      MoreSetting,
      BtnItem
    },
    props: {
      showBtns: {
        type: Array,
        default: () => {
          return [];
        }
      }
    },
    data() {
      return {
        isShowMenu: false
      };
    },
    computed: {
      ...mapState(useNewButtonStore, ['btnList']),
      btnDatas() {
        return this.btnList.filter((f) => this.showBtns.indexOf(f.type) !== -1);
      },
      orderedBtnList(){
        return this.showBtns;
      }
    },
    watch: {
      isShowMenu(val) {
        if (val) {
          document.body.addEventListener('click', this.bodyClick);
        } else {
          document.body.removeEventListener('click', this.bodyClick);
        }
      }
    },
    beforeDestroy() {
      document.body.removeEventListener('click', this.bodyClick);
    },
    mounted() {
      emitter.subscribe('btnIconSelect-clicked', ()=>this.isShowMenu = false);
      this.bodyClick = (e) => {
        if (!this.$el.contains(e.target) && !this.$refs.menu.contains(e.target)) {
          this.isShowMenu = false;
        }
      };
    },
    methods: {
      openMenu() {
        this.isShowMenu = !this.isShowMenu;
      }
    }
  };
</script>

<style lang="scss" scoped>
  .btn-icon-select {
    z-index: 100;
    .cap4-mult-select_inner__menu {
      position: absolute;
      right: -16px;
      top: 30px;
      width: 150px;
      background-color: #fff;
      box-shadow: 0 9px 28px 8px #0000000d;
      border-radius: 2px;
      ul {
        li {
          padding: 10px 13px;
          width: 100%;
          cursor: pointer;
          color: #666;
          &:hover {
            background-color: #e6f6ff;
            color: #121212;
            font-weight: bold;
          }
        }
      }
    }

    .btn-icon-select_inner__icon {
      &:hover {
        cursor: pointer;
        color: #40a9ff;
        color: var(--theme-brand4, #40a9ff);
      }
    }
  }
</style>
